var showList;
var show;
var playlist;
var currentSong;

function getTimeString(nSec){
	seconds = nSec % 60;
	minutes = (nSec - seconds) / 60;
	
	if (seconds.toString().length == 1) {
		seconds = "0" + seconds;
	}
	return minutes + ":" + seconds;
}

function populateShowListSelector(list){
	$("#show_selector option").remove();
	for(i in list){
		var itm = list[i];
		$("#show_selector").append('<option value="' + itm["ID"] + '">' + itm["NAME"] + '</option>');
	}
}

function fillShowData(data) {
	$("#show_description").val(show["DESCRIPTION"]);
}
function fillSong(song) {
	$("#playlist li").removeClass("active");
	$("#song_"+song["ID"]).addClass("active");
	currentSong = song;
	$("#song_artist").val(song["ARTIST"]);
	$("#song_title").val(song["TITLE"]);
	$("#song_year").val(song["YEAR"]);
	$("#song_length").html(getTimeString(song["LENGTH"]));
	$("#song_fname").html(song["FILENAME"]);
	$("#song_description").val(song["DESCRIPTION"]);
}
function selectSongCreator(song){ return function() { fillSong(song) }; }

function fillSongs(data) {
        $("#playlist li").remove();
        for(i in data){
                var itm = data[i];
                var li = $('<li id="song_'+ itm["ID"] +'">' + itm["ARTIST"]+ " - " + itm["TITLE"] + " (" + getTimeString(itm["LENGTH"]) + ")</li>");
                li.click(selectSongCreator(itm));
                $("#playlist").append(li);
        }
}


function dataHandler(key, data) {
	if(key == "showlist") {
		showList = data;
		populateShowListSelector(data);
	}
	if(key == "show"){
		show = data;
		fillShowData(data);
	}
	if(key == "songs"){
		playlist = data;
		fillSongs(data);
	}
	if(key == "song"){
		fillSong(data);
	}
}
